package com.hzj.mysqlite;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener, OnItemClickListener, OnItemLongClickListener
{

	ArrayList<Type> types = new ArrayList<Type>();
	Button button1;
	ListView listView1;
	MyDB myDB;

	class ViewHolder
	{
		public TextView textView1;
		public TextView textView2;

	}

	BaseAdapter baseAdapter = new BaseAdapter()
	{

		ViewHolder viewHolder;

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if (convertView == null)
			{
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);

				viewHolder = new ViewHolder();
				viewHolder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
				viewHolder.textView2 = (TextView) convertView.findViewById(R.id.textView2);

				convertView.setTag(viewHolder);
			}

			viewHolder = (ViewHolder) convertView.getTag();

			viewHolder.textView1.setText(types.get(position).getId() + "");
			viewHolder.textView2.setText(types.get(position).getName());

			return convertView;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return types.get(position);
		}

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return types.size();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) this.findViewById(R.id.button1);
		button1.setOnClickListener(this);
		listView1 = (ListView) findViewById(R.id.listView1);
		listView1.setAdapter(baseAdapter);
		listView1.setOnItemClickListener(this);
		listView1.setOnItemLongClickListener(this);

		// 这里实际上没有创建数据库
		myDB = new MyDB(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		int id = item.getItemId();
		if (id == R.id.action_settings) { return true; }
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.button1:
			button1();
			break;

		default:
			break;
		}
	}

	private void button1()
	{
		// MyDB的执行，创建数据库,并且执行初始化，需要执行onCreate。但是不是直接执行onCreate
		// myDB对象执行了getReadableDatabase或者getWritableDatabase获取了SQLiteDatabase对象后，就会立即执行onCreate
		// 如果有数据库，那么拿到SQLiteDatabase对象后可以直接操作数据库
		// 这里需要注意的是事务、关闭数据库都需要操作了
		SQLiteDatabase sqLiteDatabase = myDB.getReadableDatabase();
		types.clear();
		// 读取
		Cursor cursor = sqLiteDatabase.query(false, "type", new String[]
		{ "id", "name" }, null, null, null, null, null, null);
		while (cursor.moveToNext())
		{
			Type type = new Type();

			type.setId(cursor.getInt(0));
			type.setName(cursor.getString(1));
			types.add(type);
		}
		sqLiteDatabase.close();

		baseAdapter.notifyDataSetChanged();
	}

	// onItemLongClick返回的boolean如果设置为true，那么长按不会触发短按
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
	{

		Type temp = types.get(position);
		Log.i("hzj", "onItemLongClick");
		Intent intent = new Intent(getApplicationContext(), EditAct.class);

		intent.putExtra("type", temp);
		intent.putExtra("arrayListPosition", position);

		Log.i("hzj", "sssss:" + temp.getName());

		// startActivity(intent);

		startActivityForResult(intent, 1);

		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		Log.i("hzj", "onItemClick");
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2)
	{

		if (arg0 == 1 && arg1 == 2)
		{
			if (arg2 != null)
			{

				Type type = (Type) arg2.getSerializableExtra("type");
				int position = arg2.getIntExtra("arrayListPosition", -1);

				if (type == null || position == -1)
				{
					Log.i("hzj", "错误错误错误");
					return;
				}

				SQLiteDatabase sqLiteDatabase = myDB.getReadableDatabase();
				sqLiteDatabase.beginTransaction();
				try
				{
					ContentValues values = new ContentValues();
					values.put("name", type.getName());
					sqLiteDatabase.update("type", values, "id=?", new String[]
					{ type.getId() + "" });
					sqLiteDatabase.setTransactionSuccessful();
				}
				catch (Exception e)
				{
					Log.i("hzj", e.getMessage());
				}

				finally

				{
					sqLiteDatabase.endTransaction();
					sqLiteDatabase.close();
				}
				// 修改集合
				// 替换
				types.set(position, type);
				baseAdapter.notifyDataSetChanged();
				// 修改适配器
			}
		}
		super.onActivityResult(arg0, arg1, arg2);
	}
}
